/**
* putLocalStorage 把数据放到本地存储
* @param {*} key
* @param {*} value
*/
export const putLocalStorage = (key, value) => {
    localStorage.setItem(key, value);
};

/**
 * getLocalStorage 获取本地存储数据
 * @param {*} key
 */
export const getLocalStorage = (key) => {
    var obj = localStorage.getItem(key);
    if (obj && obj != 'undefined' && obj != 'null') {
        return obj;
    }
    return undefined;
};
/**
 * getLocalStorage 获取本地存储数据
 * @param {*} key
 */
export const getLocalStorageByKey = (key) => {
    var dataSource = getLocalStorage('dx_swagger_ui_cache');
    if (dataSource != undefined && dataSource != '') {
        var objs = JSON.parse(dataSource);
        for (var index in objs) {
            var item = objs[index];
            var pname = item['pname'];
            if (pname == key) {
                return item['pvalue'];
            }
        }
    }

    return '';
};

/**
 * getLocalStorage 获取本地存储数据
 * @param {*} key
 */
export const putLocalStorageForTest = (operationId, propertyName, value) => {
    if (value == undefined) {
        return;
    }
    var keyGroup = 'dx_swagger_ui_cache_test_param';
    var dataSource = getLocalStorage(keyGroup);

    if (dataSource != undefined && dataSource != '') {
        var objs = JSON.parse(dataSource);
        if (objs[operationId] == undefined || objs[operationId] == '') {
            objs[operationId] = {};
        }
        objs[operationId][propertyName] = value;
        putLocalStorage(keyGroup, JSON.stringify(objs));
    } else {
        var storeValue = {};
        var valueObj = {};
        valueObj[propertyName] = value;
        storeValue[operationId] = valueObj;
        putLocalStorage(keyGroup, JSON.stringify(storeValue));
    }
};
/**
 * getLocalStorage 获取本地存储数据
 * @param {*} key
 */
export const getLocalStorageForTest = (operationId, propertyName) => {
    var dataSource = getLocalStorage('dx_swagger_ui_cache_test_param');
    if (dataSource != undefined && dataSource != '') {
        var objs = JSON.parse(dataSource);
        var storeValue = objs[operationId];
        if (storeValue != undefined) {
            var v = storeValue[propertyName];
            if (v != undefined && v != '') {
                return v;
            } else {
                return '';
            }
        } else {
            return '';
        }
    }
    return '';
};
/**
 * clearLocalStorageForTest 清空缓存
 * @param {*} key
 */
export const clearLocalStorageForTest = (operationId) => {
    var keyGroup = 'dx_swagger_ui_cache_test_param';
    var dataSource = getLocalStorage(keyGroup);
    if (dataSource != undefined && dataSource != '') {
        var objs = JSON.parse(dataSource);
        delete objs[operationId];
        putLocalStorage(keyGroup, JSON.stringify(objs));
    }
};

